﻿/*******************************************************************
 *  Copyright(c) 2018-2018 A single spark
 *  All rights reserved.
 *
 *  文件名称:yuvmainwindow.cpp
 *  简要描述:处理菜单栏事件
 *
 *
 *  修改日期:  2018.03.06
 *  作者:      icedxu
 *  邮箱:      icedxu@foxmail.com
 *  说明:      增加了菜单栏的打开和退出功能
 ******************************************************************/

#include "yuvmainwindow.h"
#include "yuvcenterwidget.h"
#include "yuvaboutdialog.h"
#include "yuvmsgnotifysington.h"
#include <QFileDialog>

#include <QDebug>
#include <QSplitter>

#include <QMenuBar>

YUVMainWindow::YUVMainWindow(QWidget *parent)
    : QMainWindow(parent),
      m_pCenterWidget(Q_NULLPTR)
{
    m_pCenterWidget = new YUVCenterWidget(this);

    m_pMenuWidget = new QWidget();
    QHBoxLayout *pHLayoutWidget = new QHBoxLayout();
    QHBoxLayout *pHLayoutMenu = new QHBoxLayout();

    QPushButton *pBtnFile = new QPushButton(tr("&File"));
    QPushButton *pBtnTool = new QPushButton(tr("&Tool"));
    QPushButton *pBtnHelp = new QPushButton(tr("&Help"));

    pBtnFile->setFixedWidth(60);
    pBtnTool->setFixedWidth(60);
    pBtnHelp->setFixedWidth(60);

//是菜单按钮扁平化
    pBtnFile->setFlat(true);
    pBtnTool->setFlat(true);
    pBtnHelp->setFlat(true);

//去掉菜单按钮的小三角形
    pBtnFile->setStyleSheet("QPushButton::menu-indicator{image:none}");
    pBtnTool->setStyleSheet("QPushButton::menu-indicator{image:none}");
    pBtnHelp->setStyleSheet("QPushButton::menu-indicator{image:none}");

//将按钮与菜单联系起来
    pFileMenu = new QMenu();
    pBtnFile->setMenu(pFileMenu);
    pToolMenu = new QMenu();
    pBtnTool->setMenu(pToolMenu);
    pHelpMenu = new QMenu();
    pBtnHelp->setMenu(pHelpMenu);


    pHLayoutWidget->addWidget(m_pMenuWidget);

    pHLayoutMenu->setSpacing(0);
    pHLayoutMenu->setMargin(0);
    pHLayoutMenu->addWidget(pBtnFile);
    pHLayoutMenu->addWidget(pBtnTool);
    pHLayoutMenu->addWidget(pBtnHelp);
    pHLayoutMenu->addStretch(0);
    m_pMenuWidget->setLayout(pHLayoutMenu);


    QSplitter *splitter= new QSplitter(Qt::Vertical); //使用分割器
    splitter->addWidget(m_pMenuWidget);
    splitter->addWidget(m_pCenterWidget);
    setCentralWidget(splitter);
    for(int i = 0; i<splitter->count();i++)
    {
        QSplitterHandle *handl = splitter->handle(i);
        handl->setEnabled(false);
    }

    createFileMenu();
    createToolMenu();
    createHelpMenu();

    setAttribute(Qt::WA_StyledBackground);
    setStyleSheet(QLatin1String(Utils::FileReader::fetchQrc(":/resource/zh_ch/qss/mainWindow.css")));
}

YUVMainWindow::~YUVMainWindow()
{

}

void YUVMainWindow::createFileMenu()
{
     m_pOpen = new QAction(tr("Open"), this);
     m_pExit = new QAction(tr("&Exit"), this);

     pFileMenu->addAction(m_pOpen);
     pFileMenu->addAction(m_pExit);

     connect(m_pOpen,SIGNAL(triggered()),this,SLOT(triggeredOpen()));
     connect(m_pExit,SIGNAL(triggered()),this,SLOT(triggeredExit()));

}

void YUVMainWindow::triggeredOpen()
{
  m_OpenFilePath = QFileDialog::getOpenFileName(this,tr("select file"),"","");
  //qDebug() << "----------" <<m_OpenFilePath;
  YUVMsgNotifySington::instance()->notifyPlayListUpdate(m_OpenFilePath);

}

void YUVMainWindow::triggeredExit()
{
    qApp->quit();
}


void YUVMainWindow::createToolMenu()
{
    pToolMenu->addAction(tr(""));

}

void YUVMainWindow::createHelpMenu()
{
     connect(pHelpMenu->addAction(tr("&About")), &QAction::triggered,
             [this](bool){
                 YUVAboutDialog aboutDialog(this);
                 aboutDialog.exec();
             });
}

